<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Resources\Example;

use App\Http\Resources\BaseResource;
use App\Services\AttachmentService;
use App\Services\Example\ExampleTreeTableService;


/**
 * 普通表格演示模块资源
 * Class ExampleTableResource
 * @package App\Http\Resources
 */
class ExampleTableResource extends BaseResource
{
    /**
     * @param \Illuminate\Http\Request $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {


        $category_id = $this->exampleTreeTable ? $this->exampleTreeTable->id : 0;
        $cascader_category_ids = [];
        if($category_id > 0)  {
            $getParentTree = ExampleTreeTableService::getParentTree($category_id);
            $cascader_category_ids = array_merge($getParentTree['parent_ids'], [$category_id]);
        }

        return $this->filterFields([
            'id' => $this->id,
            'category_id' => $category_id,
            'switch' => $this->switch,
            'title' => $this->title,
            'cover_image' => AttachmentService::previewList($this->cover_image),
            'keywords' => trim($this->keywords) ? explode(",", $this->keywords) : [],
            'description' => $this->description,
            'content' => $this->content,
            'recommend_type' => trim($this->recommend_type) ? explode(",", $this->recommend_type) : [],
            'start_time' => $this->start_time ? $this->start_time->format('Y-m-d H:i:s') : "",
            'end_time' => $this->end_time ? $this->end_time->format('Y-m-d H:i:s') : "",
            'type' => $this->type,
            'tree' => trim($this->tree) ? explode(",", $this->tree) : [],
            'file_ids' => AttachmentService::previewList($this->file_ids),
            'province_id' => $this->province_id,
            'city_id' => $this->city_id,
            'county_id' => $this->county_id,
            'icon' => $this->icon,
            'href' => $this->href,
            'counts' => $this->counts,
            'image_list' => AttachmentService::getReponsePicture($this->image_list),
            'status' => $this->status,
            'created_at' => $this->created_at ? $this->created_at->format('Y-m-d H:i:s') : "",
            'updated_at' => $this->updated_at ? $this->updated_at->format('Y-m-d H:i:s') : "",
            'cascader_category_ids' =>  $cascader_category_ids
        ]);
    }
}
